discard

	set more off

/** this do-file merges together the variables for self-reporting of
	health conditions using the end user license core data files and IFS
	derived variables. **/

	* index file
		use "M:\ELSA\data\index_file_wave_0-wave_5_v2.dta" , clear

		keep idauniq sex /* hseyr hseint maincod */ finstat?? /* yrdeath mortstat*/  ///
						prodw? outnrsw?

	* merge in wave 1 information

		local wave1vars  												///
			/* interview and sample details */							///
				iintdtm iintdty w1wgt askpx1							///
			/* other information */										///
				indager   												///
			/* ever diagnosed with cvd conditions */					///
				hedim??							  						///
			/* ever diagnosed with chronic conditions */				///
				hedib?? 												///
			/* ever diagnosed with eye condition? */					///
				heopt? 							 						///
			/* angina/heart attack in last two years? */				///
				heyra heyrb												///
			/* age when first told about diagnosis */					///
				heag? heprk												///
			/* taking medication for condition? */						///
				helng heama hecanb hemda heins hemdb



		merge 1:1 idauniq using "M:\ELSA\data\Wave_1_Core_data_v3.dta" , ///
				keepusing(`wave1vars') assert(1 3) gen(inwave1)
			recode inwave1 (1=0) (3=1)
			lab define inwave 0 "Not interviewed" 1 "Interviewed"
			lab val inwave1 inwave

			/* rename everything */
				foreach x of var `wave1vars' {
					ren `x' `x'w1
				}

merge 1:1 idauniq using "M:\ELSA\data\wave_1_ifs_derived_variables.dta", keepusing(age) assert(1 3)
rename age agew1


	* merge in wave 2 information

		local wave2vars 												///
			/* interview and sample details */							///
				iintdtm iintdty w2wgt askpx1							///
			/* other information */										///
				indager   												///
			/* feed-forward information */								///
				hefflag ffprob bheopt? hedibw? hediaw?					///
			/* confirms previous report of diagnosis */ 				///
				hediac? hediad? heopc									///
			/* dispute information */									///
				hedian? hediam? heopw?	heopn?			///heopn?				///
			/* ever diagnosed */										///
				heacd hedim0? hedib0? heopt?							///
			/* still has condition */									///
				hedias? hedids? heops									///
			/* angina/heart attack/stroke in last two years? */			///
				heyra heyrb	henmmi henmst								///
			/* age first diagnosed */									///
				heag? heprk 											///
			/* time first diagnosed */									///
				heag?r heag?ry heprkr heprkry 			///
			/* taking medication for condition? */						///
				helng heama heanb hemda hemdb heins hebetb					///
				hehrtb* hehrta* hepsya hecanb


		merge 1:1 idauniq using "M:\ELSA\data\Wave_2_Core_Data_v4.dta" , ///
				keepusing(`wave2vars') assert(1 3) gen(inwave2)
			recode inwave2 (1=0) (3=1)
			lab val inwave2 inwave

			/* rename everything */
				foreach x of var `wave2vars' {
					ren `x' `x'w2
					ren `x'w2, lower
				}
				
cap drop _merge
merge 1:1 idauniq using "M:\ELSA\data\wave_2_ifs_derived_variables.dta", keepusing(age) assert(1 3)
rename age agew2

	* merge in wave 3 information

		local wave3vars 												///
			/* interview and sample details */							///
				iintdatm iintdaty w3xwgt askpx							///
			/* other information */										///
				indager   												///
			/* feed-forward information */								///
				heopf?? hedaw?? hedbw?? cheacd 							///
			/* confirms previous report of diagnosis */ 				///
				hedac?? hedbd?? heopc??									///
			/* dispute information */									///
				hedan??	hedbm?? heopn??									///
			/* ever diagnosed */										///
				/* there are derived variables giving when the
				   diagnosis was reported. Ignore these as they
				   don't include disputes. */ 							///
				dheacd dhedim?? dhedib?? hepsy?? heleuk					///
				hecanaa dheopt?? hedibbl 								///
			/* still has condition */									///
				hedbs??	hedas?? heops??  								///
			/* angina/heart attack/stroke in last two years? */			///
				heyra henmst henmmi										///
			/* age first diagnosed */									///
				heag? heprk 											///
			/* time first diagnosed */									///
				heag?r heag?ry heprkr heprkry  							///
			/* taking medication for condition? */						///
				helng helngb heama heamb hecanb hecanbb 				///
				hemda hemdab heins hemdb hechmd	hechme					///
				hehrtmd

			merge 1:1 idauniq using "M:\ELSA\data\Wave_3_elsa_data_v4.dta" , ///
				keepusing(`wave3vars') assert(1 3) gen(inwave3)
			recode inwave3 (1=0) (3=1)
			lab val inwave3 inwave

			/* rename everything */
				foreach x of var `wave3vars' {
					ren `x' `x'w3
					ren `x'w3, lower
				}
cap drop _merge				
merge 1:1 idauniq using "M:\ELSA\data\wave_3_ifs_derived_variables.dta", keepusing(age) assert(1 3)
rename age agew3
				
				
	* merge in wave 4 information

		local wave4vars 												///
			/* interview and sample details */							///
				iintdatm iintdaty w4xwgt askpx							///
			/* other information */										///
				indager   												///
			/* feed-forward information */								///
				heopf?? hedaw?? hedbw?? dheacd			 				///
			/* confirms previous report of diagnosis */ 				///
				hedac?? hedbd?? heopc??									///
			/* dispute information */									///
				hedan??	hedbm?? heopn??									///
			/* ever diagnosed */										///
				/* there are derived variables giving when the
				   diagnosis was reported. Ignore these as they
				   don't include disputes. */ 							///
				hedim?? hedib?? hepsy?? heleuk							///
				hecanaa heopt??	hefrac	hedizm51 hedizm52				///
			/* still has condition */									///
				hedbs??	hedas?? heops??  								///
			/* angina/heart attack/stroke in last two years? */			///
				heyra henmst henmmi	heyrc								///
			/* age first diagnosed */									///
				heag? heprk 											///
			/* time first diagnosed */									///
				heag?r heag?ry heprkr heprkry  							///
			/* taking medication for condition? */						///
				hepsy? helng helngb heama heamb hecanb hecanbb 			///
				hemda hemdab heins hemdb hechmd	hechme					///
				hehrtmd

		merge 1:1 idauniq using "M:\ELSA\data\Wave_4_elsa_data_v3.dta" , ///
				keepusing(`wave4vars') assert(1 3) gen(inwave4)
			recode inwave4 (1=0) (3=1)
			lab val inwave4 inwave

			/* rename everything */
				foreach x of var `wave4vars' {
					ren `x' `x'w4
					ren `x'w4, lower
				}
cap drop _merge
merge 1:1 idauniq using "M:\ELSA\data\wave_4_ifs_derived_variables.dta", keepusing(age) assert(1 3)
rename age agew4

	* merge in wave 5 information

		local wave5vars 											///
			/* interview and sample details */							///
				iintdatm iintdaty w5xwgt askpx							///
			/* other information */										///
				indager   												///
			/* feed-forward information */								///
				heopf?? hedaw?? hedbw?? dheacd			 				///
			/* confirms previous report of diagnosis */ 				///
				hedac?? hedbd?? heopc??									///
			/* dispute information */									///
				hedan??	hedbm?? heopn??									///
			/* ever diagnosed */										///
				/* there are derived variables giving when the
				   diagnosis was reported. Ignore these as they
				   don't include disputes. */ 							///
				heacd hedim?? hedib?? hepsy?? heleuk					///
				hecanaa heopt??	hefrac									///
			/* still has condition */									///
				hedbs??	hedas?? heops??  								///
			/* angina/heart attack/stroke in last two years? */			///
				heyra henmst henmmi	heyrc								///
			/* age first diagnosed */									///
				heag? heprk 											///
			/* time first diagnosed */									///
				heag?r heag?ry heprkr heprkry  							///
			/* taking medication for condition? */						///
				helng helngb heama heamb hecanb hecanbb 				///
				hemda hemdab heins hemdb hechmd	hechme					///
				hehrtmd	heostec hehrtb hehrtb2 hehrtc

		merge 1:1 idauniq using  "M:\ELSA\data\wave_5_elsa_data_v4.dta" , ///
				keepusing(`wave5vars') assert(1 3) gen(inwave5)
			recode inwave5 (1=0) (3=1)
			lab val inwave5 inwave

			/* rename everything */
				foreach x of var `wave5vars' {
					ren `x' `x'w5
					ren `x'w5, lower
				}
cap drop _merge
merge 1:1 idauniq using "M:\ELSA\data\wave_5_ifs_derived_variables.dta", keepusing(age) assert(1 3)
rename age agew5

		local wave6vars 												///
			/* interview and sample details */							///
				iintdatm iintdaty w6xwgt askpx finstat					///
			/* other information */										///
				indager   												///
			/* feed-forward information */								///
				heopf?? hedaw?? hedbw?? dheacd			 				///
			/* confirms previous report of diagnosis */ 				///
				hedac?? hedbd?? heopc??									///
			/* dispute information */									///
				hedan??	hedbm?? heopn??									///
			/* ever diagnosed */										///
				/* there are derived variables giving when the
				   diagnosis was reported. Ignore these as they
				   don't include disputes. */ 							///
				heacd hedim?? hedib?? hepsy?? heleuk					///
				hecanaa heopt??	hefrac												///
			/* still has condition */									///
				hedbs??	hedas?? heops??  								///
			/* angina/heart attack/stroke in last two years? */			///
				heyra henmst henmmi	heyrc								///
			/* age first diagnosed */									///
				heag? heprk 											///
			/* time first diagnosed */									///
				heag?r heag?ry heprkr heprkry  			///
			/* taking medication for condition? */						///
				helng helngb heama heamb hecanb hecanbb 				///
				hemda hemdab heins hemdb hechmd	hechme 					///
				heostec hehrta hehrtb hehrtc

	merge 1:1 idauniq using  "M:\ELSA\data\wave_6_elsa_data_v2.dta" , ///
				keepusing(`wave6vars')  gen(inwave6)
			recode inwave6 (1=0) (2/3=1)
			lab val inwave6 inwave

			/* rename everything */

			ren finstatw6 finstat
				foreach x of var `wave6vars' {
					ren `x' `x'w6
					ren `x'w6, lower
				}
cap drop _merge
merge 1:1 idauniq using "M:\ELSA\data\wave_6_ifs_derived_variables.dta", keepusing(age) assert(1 3)
rename age agew6
				
	* wave 7

	local wave7vars 												///
			/* interview and sample details */							///
				iintdatm iintdaty w7xwgt askpx finstat					///
			/* other information */										///
				indager   												///
			/* feed-forward information */								///
				heopf?? hedaw?? hedbw?? 		 		 				///		No variable for Whether ever told have diabetes (feed forward)
			/* confirms previous report of diagnosis */ 				///
				hedac?? hedbd?? heopc??									///
			/* dispute information */									///
				hedan??	hedbm?? heopn??									///
			/* ever diagnosed */										///
				/* there are derived variables giving when the
				   diagnosis was reported. Ignore these as they
				   don't include disputes. */ 							///
				heacd hedim?? hedib?? hepsy?? heleuk					///
				hecanaa heopt??	hefrac												///
			/* still has condition */									///
				hedbs??	hedas?? heops??  								///
			/* angina/heart attack/stroke in last two years? */			///
				heyra henmst henmmi	heyrc								///
			/* age first diagnosed */									///
				heag? heprk 											///
			/* time first diagnosed */									///
				heag?r heag?ry heprkr heprkry  			///
			/* taking medication for condition? */						///
				helng helngb heama heamb hecanb hecanbb 				///
				hemda hemdab heins hemdb hechmd	hechme 					///
				heostec hehrta hehrtb hehrtc

	merge 1:1 idauniq using  "M:\ELSA\data\wave_7_elsa_data.dta"  , ///
				keepusing(`wave7vars')  gen(inwave7)
			recode inwave7 (1=0) (2/3=1)
			lab val inwave7 inwave

			/* rename everything */

			ren finstatw7 finstat
				foreach x of var `wave7vars' {
					ren `x' `x'w7
					ren `x'w7, lower
				}
cap drop _merge
merge 1:1 idauniq using "M:\ELSA\data\wave_7_ifs_derived_variables.dta", keepusing(age) assert(1 3)
rename age agew7

	* end of wave 7

	* merge in wave 0 information

	local commonwave0vars 						///
				/* interview year */			///
					hseyr 						///
				/* reported illnesses */		///
					illsm? 	longill				///
				/* taking medication */			///
					bpmedd

	merge 1:1 idauniq using  "M:\ELSA\data\wave_0_common_variables_v2.dta" , ///
				keepusing(`commonwave0vars')  gen(inwave0) keep(match master)
			recode inwave0 (1=0) (3=1)
			lab val inwave0 inwave

			/* rename everything */
				foreach x of var `commonwave0vars' {
					ren `x' `x'w0
				}

	local wave0vars_98_99 								///
				/* whether hypertensive */				///
					hyper1 highbp1 hyper2 highbp2 		///
					hyper140 hibp140 nopregbp stillbp	///
				/* mental disorders */					///
					compm3								///
				/* ever diagnosed with things */		///
					bp1	heartdef  						///
					angidef iregdef ohtdef strodef		///
					diabete2 murmur1					///
				/* age diagnosed with things */			///
					ageangi ageinfbp ageinfo1 ageheart  ///
					agestro ageinmur ageireg			///
				/* medication */						///
					murpill medcindi medheart pastabbp  ///
					admur mursurg insulin advicedi  ///
				/* vars to rule out stroke medication for heart conditions */	///
					everhart everangi everireg everoht everstro

	 merge 1:1 idauniq using "M:\ELSA\data\wave_0_1998_data.dta" , ///
				keepusing(`wave0vars_98_99') nogen keep(match master)

			/* rename everything */
				foreach x of var `wave0vars_98_99' {
					ren `x' `x'w0
				}

	 merge 1:1 idauniq using "M:\ELSA\data\wave_0_1999_data.dta" , ///
				keepusing(`wave0vars_98_99') nogen keep(match master)

			/* rename everything */
				foreach x of var `wave0vars_98_99' {
					replace `x'w0 = `x' if `x'w0==.
					drop `x'
				}

	local wave0vars_2001 								///
				/* whether hypertensive */				///
					hyper1 highbp1 hyper2 highbp2 		///
					hyper140 hibp140 					///
				/* mental disorders */					///
					compm3


	merge 1:1 idauniq using "M:\ELSA\data\wave_0_2001_data.dta" , ///
			keepusing(`wave0vars_2001') nogen keep(match master)

		/* rename everything */
			foreach x of var `wave0vars_2001' {
				replace `x'w0 = `x' if `x'w0==.
				drop `x'
			}

	local wave0vars_refresher3and4 						///
				/* whether hypertensive */				///
					stillbp	///
				/* ever diagnosed with things */		///
					docangi bp1	heartdef  				///
					strodef	diabete2 					///
				/* age diagnosed with things */			///
					ageangi ageinfbp ageinfo1 ageheart  ///
					agestro ageinmur ageireg

	merge 1:1 idauniq using "M:\ELSA\data\Collaborators\refreshFF\elsa_cohort3and4_refreshment_data" , ///
			keepusing(`wave0vars_refresher3and4') nogen keep(match master)



			/* rename everything */
			foreach x of var `wave0vars_refresher3and4' {
				if "`x'" != "docangi" {
					replace `x'w0 = `x' if `x'w0==.
				}
				else {
					replace angidefw0 = docangi if angidefw0 == .
				}
				drop `x'
			}


	/** NURSE VISITS **/

	*wave
		local wave2nursevars bpconst bprespc respbps diaval sysval

		merge 1:1 idauniq using "M:\ELSA\data\wave_2_nurse_data_v2.dta" ,  ///
			keepusing(`wave2nursevars') nogen
			gen in_nurse_dataw2 = (outnrsw2==81)

			foreach x of varlist `wave2nursevars' {
				ren `x' `x'w2
			}

		local wave4nursevars bpconst bprespc respbps diaval sysval

		merge 1:1 idauniq using "M:\ELSA\data\wave_4_nurse_data.dta", ///
          keepusing(`wave4nursevars') nogen
		gen in_nurse_dataw4 = (outnrsw4==80 | outnrsw4==81)

			foreach x of varlist `wave4nursevars' {
				ren `x' `x'w4
			}

		local wave6nursevars bpconst respbps diaval sysval

		merge 1:1 idauniq using "M:\ELSA\data\wave_6_elsa_nurse_data_v2.dta", ///
          keepusing(`wave6nursevars') nogen
		gen in_nurse_dataw6 = (outnrsw4==80 | outnrsw4==81)

			foreach x of varlist `wave6nursevars' {
				ren `x' `x'w6
				ren `x'w6, lower
			}




	/** ditch non-respondents **/

		egen interviewed_at_all = anymatch(inwave1 inwave2 inwave3 inwave4 inwave5 inwave6 inwave7) , v(1)
		ta interviewed_at_all
		drop if interviewed_at_all==0
		drop interviewed_at_all


	/** get information on people's ages **/

		merge 1:1 idauniq using  "M:\ELSA\data\Collaborators\DoB\ELSA_W1-7_dob.dta" , ///
				keep(match master) nogen


		* NOTE: GEORGE HAS CHANGED THIS COMPLETELY ON 19/04/17 - ADDS AGE VARIABLE FROM DERIVED VARIABLES 
		** 501 people without DoB in wave 7 as still waiting on this data

		** 2 people in wave 3 with missing age
		** 12 people in wave 4 with missing age
		** 1,194 people in wave 6 with missing age
		** No ages for wave 7 as don't have interview date yet

		replace agew3 = indagerw3 if agew3<0 & indagerw3>0
		replace agew4 = indagerw4 if agew4<0
		replace agew6 = indagerw6 if agew6<0

		** 1 person with missing age in w3. annoying.  THEY ALSO ARE MISSING ALL INFO - DROP.
		drop if idauniq==121375 
		*/
				

	*	drop indager* dob

		forv i = 1/5 {
			replace inwave`i' = 0 if inwave`i'==. & (inwave6==1 | inwave7==1)
		}
		* Is there a reason why this is not if inwave`i'==. & inwave*==1 for *>i ?
		saveold "$healthsave\merged health data_w7.dta", replace
